package com.czl.module_storehouse.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.view.View;


import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.base.data.bean.tengyun.SortBeanType;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.listener.OnItemChangeListener;
import com.lihang.ShadowLayout;

import java.util.List;

/**
 * @author xubin
 */
public class AllocateAddAdapter extends BaseMultiItemQuickAdapter<SortBean, BaseViewHolder> {

    private final OnItemChangeListener mOnItemChangeListener;

    public AllocateAddAdapter(@Nullable List<SortBean> data, OnItemChangeListener onItemChangeListener) {
        super(data);
        addItemType(SortBeanType.Type.VIEW_TYPE_SORT, R.layout.item_allocate_add);
        addItemType(SortBeanType.Type.VIEW_TYPE_PRODUCT, R.layout.item_receive_product);
        mOnItemChangeListener = onItemChangeListener;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, SortBean item) {
        switch (item.getItemType()) {
            case SortBeanType.Type.VIEW_TYPE_SORT:
                helper.setText(R.id.tv_title, item.getSortName())
                        .setText(R.id.tv_model, "型号：" + item.getSortModel())
                        .setText(R.id.tv_allocate_num, String.valueOf(item.getAllocateCount()));
                helper.getView(R.id.iv_delete).setOnClickListener((View.OnClickListener) v -> {
                    remove(item);
                    if (mOnItemChangeListener != null) {
                        mOnItemChangeListener.remove(helper.getLayoutPosition() - getHeaderLayoutCount());
                    }
                });
                ShadowLayout shadowLayout = helper.getView(R.id.shadowLayout);
                shadowLayout.setShadowHiddenTop(helper.getLayoutPosition() - getHeaderLayoutCount() != 0);
                shadowLayout.setOnClickListener(v -> {
                    if (getOnItemChildClickListener() != null) {
                        getOnItemChildClickListener().onItemChildClick(AllocateAddAdapter.this, v, helper.getLayoutPosition() - getHeaderLayoutCount());
                    }
                });
                break;
            case SortBeanType.Type.VIEW_TYPE_PRODUCT:
                ProductBean productBean = item.getProductBean();
                if (productBean == null) {
                    return;
                }
                helper.setText(R.id.tv_title, productBean.getSortName())
                        .setText(R.id.tv_model, "型号：" + productBean.getProductModel())
                        .setText(R.id.tv_code, "编号：" + productBean.getProductCode())
                        .setText(R.id.tv_loc, "位置：" + productBean.getStorehouseLocatName());
                helper.getView(R.id.tv_remove)
                        .setOnClickListener(v -> {
                            remove(item);
                            if (mOnItemChangeListener != null) {
                                mOnItemChangeListener.remove(helper.getLayoutPosition() - getHeaderLayoutCount());
                            }
                        });
                break;
            default:
                break;
        }
    }

    @Override
    public void addData(@NonNull SortBean data) {
        super.addData(data);
        if (mOnItemChangeListener != null) {
            mOnItemChangeListener.add(getData().size() + getHeaderLayoutCount());
        }
    }
}
